﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using Npgsql;

namespace CREAPA.Repositorio
{
  public class ContextoPostgres : IDisposable
    {
      private readonly NpgsqlConnection minhaConexao;

        public ContextoPostgres()
        {
            minhaConexao = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["StringConexaoPostgres"].ConnectionString);  
            minhaConexao.Open();
            
        }
    
        public void ExecutaComando(string strQuery)
        {

            var cmdComando = new Npgsql.NpgsqlCommand

            {
                CommandText = strQuery,
                CommandType = CommandType.Text,
                Connection = minhaConexao

            };
            cmdComando.ExecuteNonQuery();
        }


        public NpgsqlDataReader ExecutaComandoComRetorno(string strQuery)
        {
            var cmdComando = new Npgsql.NpgsqlCommand(strQuery, minhaConexao);
            cmdComando.CommandTimeout = 2 * 60;  // 2 min
            return cmdComando.ExecuteReader();

        }
        public void Dispoded()
        {
            if (minhaConexao.State == ConnectionState.Open)
            {
                minhaConexao.Close();
                {

                }
            }
        }

        public void Dispose()
        {
            if (minhaConexao.State == ConnectionState.Open)
            {
                minhaConexao.Close();
                {

                }
            }
        }

    }
}
